﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace comix.admininstration
{
    public partial class series : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void SelectItem(DropDownList ddl, int value)
        {
            for (int i = 0; i < ddl.Items.Count; i++)
            {
                if (ddl.Items[i].Value == value.ToString())
                {
                    ddl.SelectedIndex = i;
                    break;
                }
            }
        }


        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                ListViewDataItem lvdi = (ListViewDataItem)e.Item;
                DataRowView drv = (DataRowView)lvdi.DataItem;

                DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList1");
                SelectItem(ddl, (int)drv["ser_pub_id"]);

                ddl = (DropDownList)e.Item.FindControl("DropDownList2");
                SelectItem(ddl, (int)drv["ser_cat_id"]);

                ddl = (DropDownList)e.Item.FindControl("DropDownList3");
                SelectItem(ddl, (int)drv["ser_par_id"]);
            }
        }

        protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            DropDownList ddl = (DropDownList)ListView1.EditItem.FindControl("DropDownList1");
            e.NewValues.Add("ser_pub_id", ddl.SelectedValue);

            ddl = (DropDownList)ListView1.EditItem.FindControl("DropDownList2");
            e.NewValues.Add("ser_cat_id", ddl.SelectedValue);

            ddl = (DropDownList)ListView1.EditItem.FindControl("DropDownList3");
            e.NewValues.Add("ser_par_id", ddl.SelectedValue);
        }

        protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            DropDownList ddl = (DropDownList)ListView1.InsertItem.FindControl("DropDownList1");
            e.Values.Add("ser_pub_id", ddl.SelectedValue);

            ddl = (DropDownList)ListView1.InsertItem.FindControl("DropDownList2");
            e.Values.Add("ser_cat_id", ddl.SelectedValue);

            ddl = (DropDownList)ListView1.InsertItem.FindControl("DropDownList3");
            e.Values.Add("ser_par_id", ddl.SelectedValue);
        }

        protected void InsertAuthors(object sender, CommandEventArgs e)
        {
            ListBox lb = (ListBox)ListView1.EditItem.FindControl("ListBox1");
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["mainconectionstring"].ConnectionString);
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "DECLARE @c int; SELECT @c = (SELECT COUNT(*) FROM AUTHORS2SERIES WHERE a2s_aut_id = @a2s_aut_id AND a2s_ser_id = @a2s_ser_id); ";
            com.CommandText += "IF @c=0 INSERT AUTHORS2SERIES (a2s_aut_id,a2s_ser_id,a2s_updatedate,a2s_deleted) VALUES (@a2s_aut_id, @a2s_ser_id, getDate(), 0)";
            com.Parameters.Add(new SqlParameter("@a2s_ser_id", Convert.ToInt32(e.CommandArgument)));
            com.Parameters.Add(new SqlParameter("@a2s_aut_id", SqlDbType.Int));
            con.Open();            
            for (int i = 0; i < lb.Items.Count; i++)
            {
                if (lb.Items[i].Selected)
                {
                    com.Parameters["@a2s_aut_id"].Value = Convert.ToInt32(lb.Items[i].Value);
                    com.ExecuteNonQuery();
                }
            }
            con.Close();
            Response.Redirect(Request.RawUrl);
        }

        protected void RemoveAuthors(object sender, CommandEventArgs e)
        {
            ListBox lb = (ListBox)ListView1.EditItem.FindControl("ListBox2");
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["mainconectionstring"].ConnectionString);
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "UPDATE AUTHORS2SERIES SET a2s_updatedate=getDate(), a2s_deleted=1 WHERE  a2s_aut_id=@a2s_aut_id AND a2s_ser_id = @a2s_ser_id";
            com.Parameters.Add(new SqlParameter("@a2s_ser_id", Convert.ToInt32(e.CommandArgument)));
            com.Parameters.Add(new SqlParameter("@a2s_aut_id", SqlDbType.Int));
            con.Open();
            for (int i = 0; i < lb.Items.Count; i++)
            {
                if (lb.Items[i].Selected)
                {
                    com.Parameters["@a2s_aut_id"].Value = Convert.ToInt32(lb.Items[i].Value);
                    com.ExecuteNonQuery();
                }
            }
            con.Close();
            Response.Redirect(Request.RawUrl);
        }
    }
}
